package ldh.im.fxgui.rpc.service;

import ldh.im.fxbase.dao.ImGroupDao;
import ldh.im.fxbase.dao.ImGroupUserDao;
import ldh.im.fxbase.dao.ImSessionDao;
import ldh.im.fxbase.data.event.EventBusFactory;
import ldh.im.fxbase.data.event.SessionChange;
import ldh.im.fxbase.data.pojo.ImGroup;
import ldh.im.fxbase.data.pojo.ImGroupUser;
import ldh.im.fxbase.data.pojo.ImSession;
import ldh.im.fxbase.util.BeanFactory;

import java.sql.SQLException;
import java.util.Date;

public class SessionChangeDataService {

    private ImSessionDao sessionDao = BeanFactory.getInstance().getBean(ImSessionDao.class);
    private ImGroupDao groupDao = BeanFactory.getInstance().getBean(ImGroupDao.class);
    private ImGroupUserDao groupUserDao = BeanFactory.getInstance().getBean(ImGroupUserDao.class);

    // 修改会话是否免打扰
    public void editSessionDisturb(String userId, String userIdOrGroupCode, String isDisturbStr) throws SQLException {
        boolean isDisturb = Boolean.parseBoolean(isDisturbStr);
        ImSession session = sessionDao.getByUserId(userId, userId, userIdOrGroupCode);
        if (session == null) {
            session = sessionDao.getByGroupCodeAndUserId(userIdOrGroupCode, userId);
        }

        if (!session.isP2p()) { // 群聊
            ImGroup group = groupDao.getByCodeAndUserId(session.getGroupCode(), userId);
            ImGroupUser groupUser = group.getGroupUserSet().stream().filter((gu)->gu.getUserId().equals(userId)).findFirst().get();
            groupUser.setDisturb(isDisturb);
            groupUser.setUpdateTime(new Date());
            groupUserDao.update(groupUser);

            EventBusFactory.getInstance().post(new SessionChange(session.getId()));
        } else {
            if (session.getFromUserId().compareTo(session.getToUserId())>=0) {
                session.setDisturb(isDisturb);
            } else {
                session.setDisturb2(isDisturb);
            }
            session.setUpdateTime(new Date());
            sessionDao.updateSession(session);

            EventBusFactory.getInstance().post(new SessionChange(session.getId()));
        }
    }

    // 修改会话是否置顶
    public void editSessionTop(String userId, String userIdOrGroupCode, String isTopStr) throws SQLException {
        boolean isTop = Boolean.parseBoolean(isTopStr);
        ImSession session = sessionDao.getByUserId(userId, userId, userIdOrGroupCode);
        if (session == null) {
            session = sessionDao.getByGroupCodeAndUserId(userIdOrGroupCode, userId);
        }

        if (!session.isP2p()) { // 群聊
            ImGroup group = groupDao.getByCodeAndUserId(session.getGroupCode(), userId);
            ImGroupUser groupUser = group.getGroupUserSet().stream().filter((gu)->gu.getUserId().equals(userId)).findFirst().get();
            groupUser.setTop(isTop);
            groupUser.setUpdateTime(new Date());
            groupUserDao.update(groupUser);

            EventBusFactory.getInstance().post(new SessionChange(session.getId()));
        } else {
            if (session.getFromUserId().compareTo(session.getToUserId())>=0) {
                session.setTop(isTop);
            } else {
                session.setTop2(isTop);
            }
            session.setUpdateTime(new Date());
            sessionDao.updateSession(session);

            EventBusFactory.getInstance().post(new SessionChange(session.getId()));
        }
    }

    // 修改会话是否置顶
    public void editSessionWaking(String userId, String userIdOrGroupCode, String isWakingStr) throws SQLException {
        boolean isWaking = Boolean.parseBoolean(isWakingStr);
        ImSession session = sessionDao.getByUserId(userId, userId, userIdOrGroupCode);
        if (session == null) {
            session = sessionDao.getByGroupCodeAndUserId(userIdOrGroupCode, userId);
        }

        if (!session.isP2p()) { // 群聊
            ImGroup group = groupDao.getByCodeAndUserId(session.getGroupCode(), userId);
            ImGroupUser groupUser = group.getGroupUserSet().stream().filter((gu)->gu.getUserId().equals(userId)).findFirst().get();
            groupUser.setWaking(isWaking);
            groupUser.setUpdateTime(new Date());
            groupUserDao.update(groupUser);

            EventBusFactory.getInstance().post(new SessionChange(session.getId()));
        } else {
            if (session.getFromUserId().compareTo(session.getToUserId())>=0) {
                session.setWaking(isWaking);
            } else {
                session.setWaking2(isWaking);
            }
            session.setUpdateTime(new Date());
            sessionDao.updateSession(session);

            EventBusFactory.getInstance().post(new SessionChange(session.getId()));
        }
    }
}
